package com.spring.demo5.eg5;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.ConfigurationCondition;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Map;

/**
 * @author vhans
 */
public class MyConfigurationCondition implements ConfigurationCondition {
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        ConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory();
        Map<String, Service> beansOfType = beanFactory.getBeansOfType(Service.class);
        return !beansOfType.isEmpty();
    }

    @Override
    public ConfigurationPhase getConfigurationPhase() {
        // 指定在bean注册阶段进行条件判断
        return ConfigurationPhase.REGISTER_BEAN;
    }
}
